﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace NewDevice.XiWei
{
    /// <summary>
    /// 稀微生成ware文件操作类
    /// </summary>
    public class WareGeneratorInterop
    {
        // 假设调用约定为 cdecl，请根据实际编译设置确认
        private const string DllName = "wareGenerator.dll";

        /// <summary>
        /// 获取DLL版本号
        /// </summary>
        /// <returns>函数返回值:DLL版本号字符串</returns>
        [DllImport(DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
        public static extern IntPtr getDLLVersion();

        /// <summary>
        /// 打开一个ware模板文件
        /// </summary>
        /// <param name="path"></param>
        /// <returns>0:成功,1:文件打开失败，2:文件校验错误</returns>
        [DllImport(DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
        public static extern int openWareTemplate(string path);

        /// <summary>
        /// 另存为ware文件,传入参数为目录路径
        /// </summary>
        /// <param name="dirPath"></param>
        /// <returns>0:成功,1:目录路径不存在，2:写入文件失败</returns>
        [DllImport(DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
        public static extern int saveWareFile(string dirPath);

        /// <summary>
        /// 获取芯片信息
        /// </summary>
        /// <param name="chipInfo"></param>
        /// <returns>0:成功</returns>
        [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
        public static extern int getChipInfo(ref WareChipInfo chipInfo);

        /// <summary>
        /// 获取基础配置
        /// </summary>
        /// <param name="basicConfig"></param>
        /// <returns>0:成功</returns>
        [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
        public static extern int getBasicConfig(ref WareBasicConfig basicConfig);

        /// <summary>
        /// 修改基础配置
        /// </summary>
        /// <param name="basicConfig"></param>
        /// <returns>0:成功</returns>

        [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
        public static extern int setBasicConfig(ref WareBasicConfig basicConfig);

        /// <summary>
        /// 清除已添加的bin文件数据
        /// </summary>

        [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
        public static extern void clearBinFiles();

        /// <summary>
        /// 添加一个bin文件
        /// </summary>
        /// <param name="fileInfo"></param>
        /// <returns>0:成功,1:读取文件失败，2:当前文件数量超过10个</returns>
        [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
        public static extern int addBinFile(BinFileInfo fileInfo);
    }

    /// <summary>
    /// enum MemoryType
    /// </summary>
    public enum MemoryType : uint
    {
        FLASH = 0,
        EEPROM,
        OTP
    }

    /// <summary>
    /// 对应 struct BinFileInfo
    /// </summary>
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
    public struct BinFileInfo
    {
        /// <summary>
        /// bin文件路径
        /// </summary>
        public string filePath;   // MarshalAs 特性会在 DllImport 中处理

        /// <summary>
        /// 对应MemoryType
        /// </summary>
        public uint memtype;

        /// <summary>
        /// 文件烧录地址
        /// </summary>
        public uint addr;
    }

    /// <summary>
    /// 基础配置
    /// </summary>
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)] // 关键：显式指定打包对齐为1字节
    public struct WareBasicConfig
    {
        /// <summary>
        /// 镜像号
        /// </summary>
        public uint mirrorNumber;        // 对应 unsigned int

        /// <summary>
        /// 镜像名称，不带.ware后缀
        /// </summary>
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
        public string mirrorName;

        /// <summary>
        /// 擦除使能
        /// </summary>
        public byte eraseEnable;

        /// <summary>
        /// 查空使能
        /// </summary>
        public byte checkemptyEnable;

        /// <summary>
        /// 编程使能
        /// </summary>
        public byte programmerEnable;

        /// <summary>
        /// 校验使能
        /// </summary>
        public byte verifyEnable;

        /// <summary>
        /// 擦除模式：0-擦除全部，1-擦除扇区，2-指定擦除页，3-不擦除
        /// </summary>
        public uint eraseMode;

        /// <summary>
        /// 校验模式
        /// </summary>
        public uint verifyMode;

        /// <summary>
        /// 下载速度：0-超高速,1-高速，2-全速，3低速
        /// </summary>
        public uint downloadSpeed;

        /// <summary>
        /// 0 输出3.3V 1输出5.5V 2不输出
        /// </summary>
        public uint outputVoltage;

        /// <summary>
        /// 输出电压后多少ms开始烧录
        /// </summary>
        public uint beginDownDelay;

        /// <summary>
        /// 数量
        /// </summary>
        public uint CurrentNum;

        /// <summary>
        /// 烧录限制次数
        /// </summary>
        public uint limitNum;

        /// <summary>
        /// 0 确认全部烧录完成减次数，1 烧录失败和烧录成功都减次数
        /// </summary>
        public uint limitNumFlag;

        /// <summary>
        /// 烧录接口 0-SWD,1-JTAG,2-ISP(UART),3-SWIM,4-BDM,5-UPDI
        /// </summary>
        public uint downInterface;

        /// <summary>
        /// 0-智能复位 1-不复位 2-断电复位 3-关闭电源
        /// </summary>
        public uint resetMode;

        /// <summary>
        /// 0-不保护 ，1-保护
        /// </summary>
        public uint readProtect;

        /// <summary>
        /// 0-不加密, 1-加密
        /// </summary>
        public uint downEncryption;

        /// <summary>
        /// 加密下载的SN号
        /// </summary>
        // 注意：所有 char 数组都需要正确映射
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 12)]
        public string encryptionSN;

        /// <summary>
        /// 0-不隐藏, 1-隐藏
        /// </summary>
        public uint hideChip;

        /// <summary>
        /// 隐藏的芯片名字
        /// </summary>
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 116)]
        public string hideChipName;

        /// <summary>
        /// 绑定设备号
        /// </summary>
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 12)]
        public string devicebind;

        /// <summary>
        /// 自动机台触发模式 0是简单触发,1是复合触发
        /// </summary>
        public uint AutoStartMode;

        /// <summary>
        /// 烧录完以后,需要延迟多长时间
        /// </summary>
        public uint downoverdelay;

        /// <summary>
        /// 烧录完以后，跳转flag
        /// </summary>
        public uint downJumpflag;

        /// <summary>
        /// 跳转之后，等待的时间MS
        /// </summary>
        public uint downJumpdelay;

        /// <summary>
        /// 要跳转到下一个镜像号
        /// </summary>
        public uint downJumpnum;
    }

    // 对应 struct WareChipInfo
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
    public struct WareChipInfo
    {
        /// <summary>
        /// 芯片型号
        /// </summary>
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
        public string chipName;

        /// <summary>
        /// FLASH的起始地址
        /// </summary>
        public uint flashStart;

        /// <summary>
        /// FLASH大小
        /// </summary>
        public uint flashSize;
    }
}